package semant.firstpass;

import semant.Env;
import symbol.ClassInfo;
import symbol.Symbol;
import symbol.VarInfo;
import syntaxtree.VarDecl;
import syntaxtree.VisitorAdapter;
import util.List;

public final class AttributeHandler extends VisitorAdapter
{
	private Env e;
	private ClassInfo ci;
	
	private AttributeHandler(Env e, ClassInfo ci)
	{
		super();
		this.e = e;
		this.ci = ci;
	}
	
	public static void firstPass(Env e, ClassInfo ci, List<VarDecl> list) 
	{
		AttributeHandler h = new AttributeHandler(e, ci);
		
		for(List<VarDecl> v = list; v != null; v = v.tail)
			v.head.accept(h);
	}
	
	@Override
	public void visit(VarDecl node)
	{
		Symbol name = Symbol.symbol(node.name.s);
		
		VarInfo vi = new VarInfo(node.type, name);
		
		if(!ci.addAttribute(vi))
		{
			VarInfo old = ci.attributes.get(name);
			
			e.err.Error(node.name, new Object[]{"Atributo \'" + name + "\' redeclarado para classe \'" + ci.name + "\' , Declaracao anterior aqui: [" + old.type.line + "," + old.type.row + "]"});
		}
	}	
}
